/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package g360.WebXpdlValidation;

import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;

/**
 *
 * @author andya
 */
public class XpdlDomDocument extends DomDocument
{
    public static XpdlDomDocument make(InputStream source, ErrorHandler errorHandler) 
        throws ParserConfigurationException, SAXException, IOException, Exception
    {
        XpdlDomDocument result = null;
        /*DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        if (errorHandler != null)
        {
        db.setErrorHandler(errorHandler);
        }
        Document doc = db.parse(source);*/
        Document doc = DomDocument.parse(source, errorHandler);
        result = XpdlDomDocument.make(doc);
        return result;
    }
    
    public static XpdlDomDocument make(Document domDocument) throws Exception
    {
        XpdlDomDocument result = null;
        result = new XpdlDomDocument(domDocument);
        result.initialize();
        return result;
    }

    public static final String S_DEFAULT_NAMESPACE = "xmlns";
    public static final String S_XPDL_NAMESPACE = "xmlns:xpdl";
    public static final String S_PACKAGE = "Package";
    public final String S_PACKAGE_HEADER = "PackageHeader";
    public static final String S_CONFORMANCE_CLASS = "ConformanceClass";
    // 
    // Instance variables
    Node packageNode = null;
    
    private XpdlDomDocument(Document domDoc) 
    {
        super(domDoc);
    }
    
        public void initialize() throws Exception
    {
//        this.packageNode = this.getFirstChild(this.getDomDocument().getDocumentElement(), S_PACKAGE);
        this.packageNode = getDomDocument().getDocumentElement();
        if (this.packageNode == null)
        {
            throw new Exception("Can not find Package element.");
        }
    }
    
    public String getDefaultNamespace()
    {
        return getAttributeValue(this.packageNode, S_DEFAULT_NAMESPACE);
    }
    
    public void setDefaultNamespace(String namespaceName)
    {
        setAttribute(packageNode, S_DEFAULT_NAMESPACE, namespaceName);
    }
    
    public String getXpdlNamespace()
    {
        return getAttributeValue(packageNode, S_XPDL_NAMESPACE);
    }
    
    public void setXpdlNamespace(String namespaceName)
    {
        setAttribute(packageNode, S_XPDL_NAMESPACE, namespaceName);
    }
    
    public String getCustomNamespace(String namespaceName)
    {
        return getAttributeValue(packageNode, namespaceName);
    }
    
    public void setCustomNamespace(String namespaceName, String value)
    {
        setAttribute(packageNode, namespaceName, value);
    }
    
    private Node getConformanceClassNode()
    {
        return getFirstChild(packageNode, S_CONFORMANCE_CLASS);

    }
    
    public String getConformanceClass()
    {
        String result = null;
        Node conformanceClassNode = this.getConformanceClassNode();
        if (conformanceClassNode != null)
        {
            result = getAttributeValue(conformanceClassNode, S_CONFORMANCE_CLASS);
        }
        return result;
    }
    
    public void setConformanceClass(String value)
    {
        Node conformanceClassNode = getConformanceClassNode();
        if (conformanceClassNode == null)
        {
            // Add a conformance class node
            conformanceClassNode = getDomDocument().createElement(S_CONFORMANCE_CLASS);
            Node packageHeaderNode = getFirstChild(packageNode,S_PACKAGE_HEADER);
            insertAfter(packageNode, packageHeaderNode, conformanceClassNode);
        }
        // Set the existing conformance class node to match user specified level.
        setAttribute(conformanceClassNode, "BPMNModelPortabilityConformance", value.toUpperCase());
    }
}
